package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatementItemServiceImpl implements IStatementItemService {

    @Autowired
    private StatementItemMapper statementItemMapper;
    @Autowired
    private IStatementService statementService;


    @Override
    public TablePageInfo<StatementItem> query(StatementItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<StatementItem>(statementItemMapper.selectForList(qo));
    }


    @Override
    public void save(StatementItem statementItem) {
        statementItemMapper.insert(statementItem);
    }

    @Override
    public StatementItem get(Long id) {
        return statementItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(StatementItem statementItem) {
        statementItemMapper.updateByPrimaryKey(statementItem);
    }

    @Override
    public void deleteBatch(String id) {
        Long[] dictIds = Convert.toLongArray(id);
        for (Long dictId : dictIds) {
            statementItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<StatementItem> list() {
        return statementItemMapper.selectAll();
    }

    @Override
    @Transactional
    public void saveItems(List<StatementItem> items) {
        //获取左后一条记录
        StatementItem tempItem = items.remove(items.size() - 1);
        //获得前台传入的折扣金额
        BigDecimal disCountPrice = tempItem.getItemPrice();
        //根据id获取结算单对象
        Long statementId = tempItem.getStatementId();
        Statement statement = statementService.get(statementId);
        //合理性校验-->判断结算单的状态是否是消费中
        if (Statement.STATUS_PAID.equals(statement.getStatus())) {
            throw new BusinessException("已支付的结算单不能再编辑保存明细");
        }

        statementItemMapper.deleteByStatementId(statementId);

        //计算整个明细的总金额和总数量
        BigDecimal totalAmount = new BigDecimal("0.0");//总金额
        BigDecimal totalQuantity = new BigDecimal("0");//总数量
        for (StatementItem item : items) {
            //把每个结算单明细保存到数据库中
            statementItemMapper.insert(item);
            //明细的单价 * 数量--->累加到 总金额中
            totalAmount = totalAmount.add(item.getItemPrice().multiply(item.getItemQuantity()));
            totalQuantity = totalQuantity.add((item.getItemQuantity()));
        }
        //折扣价减总金额大于零(即折扣价大于总金额),非法操作
        if (disCountPrice.compareTo(totalAmount) > 0) {
            throw new BusinessException("非法操作,优惠金额大于总金额");
        }
        statementService.updateAmount(statementId, totalAmount, totalQuantity, disCountPrice);
    }

    @Override
    public void deleteRelaByStatementId(Long statementId) {
        statementItemMapper.deleteByStatementId(statementId);
    }
}
